package yongxin.controller.tasks;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.poi.excel.export.template.ExcelExportOfTemplateUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.hibernate.criterion.Restrictions;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import yongxin.entity.process.ProcessEntity;
import yongxin.entity.process.ProcessJsonEntity;
import yongxin.entity.process.ProcessNodeJsonEntity;
import yongxin.entity.processreport.ProcessReportEntity;
import yongxin.entity.tasks.TasksEntity;
import yongxin.service.process.ProcessServiceI;
import yongxin.service.processreport.ProcessReportServiceI;
import yongxin.service.tasks.TasksServiceI;

/**   
 * @Title: Controller
 * @Description: 工序汇报任务单
 * @author zhangdaihao
 * @date 2015-02-05 11:44:08
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/tasksController")
public class TasksController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TasksController.class);

	@Autowired
	private TasksServiceI tasksService;
	@Autowired
	private ProcessServiceI processService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private ProcessReportServiceI processReportService;
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 工序汇报任务单列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "tasks")
	public ModelAndView tasks(HttpServletRequest request) {
		String type = request.getParameter("type");
		request.setAttribute("type", type);
		if(StringUtil.isEmpty(type)){
		return new ModelAndView("yongxin/tasks/tasksList");
		}else{
			
			// 平均成材率
			/*
			String sql = "select AVG(CONVERT(numeric(19,2), (SUBSTRING(frate,1,CHARINDEX('%',frate,1)-1)))) avg_frate from t_tasks where fstate = '1'";
		//	String sql = "select cast(Convert(decimal(28,2),round(SUM(Convert(decimal(18,2),isnull(fcpzl,0)))/SUM(Convert(decimal(18,2),isnull(fweight,0)))*100,2)) as varchar(10)) + '%' avg_frate from t_tasks where fstate = '1' ";
			if(type.equals("S")){
			sql += " and fbcph like 'S%'";
			}else if(type.equals("Y")){
			sql += " and fbcph like 'Y%'";
			}
			String avg_frate = String.valueOf(tasksService.findOneForJdbc(sql).get("avg_frate"));
			avg_frate = avg_frate=="null"?"":avg_frate.substring(0, 5)+"%";
			request.setAttribute("avg_frate", avg_frate);*/			
			
	    return new ModelAndView("yongxin/reports/rateReportList");	
		}
	}  

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(TasksEntity tasks,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(TasksEntity.class, dataGrid);
		cq.addOrder("createDate", SortDirection.desc);
		//查询条件组装器
		TSUser user = ResourceUtil.getSessionUser();//(TSUser) request.getSession().getAttribute("loginUser");
		if (user.getUserName().contains("bxg"))
		{
			cq.like("fbcph", "S%");
			
		} else if (user.getUserName().contains("tg"))
		{
			cq.like("fbcph", "Y%");
		}
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tasks, request.getParameterMap());
		this.tasksService.getDataGridReturn(cq, true);
	    String total_trzl = String.valueOf(tasksService.findOneForJdbc("select sum(CONVERT(numeric(19,2),fweight)) as sum_trzl from t_tasks").get("sum_trzl"));
		
	    total_trzl = total_trzl=="null"?"":total_trzl;
		dataGrid.setFooter("fwidth:合计,fweight");
	    
	//	dataGrid.setFooter("fstate:重量总计,"+ "fbcph:"+ total_trzl +",fyclh:合计,fweight");
		//dataGrid.setFooter("fyclh:合计,fweight");
		TagUtil.datagrid(response, dataGrid);
	}

	
	/**
	 * 成材率报表
	 * 
	 * @param request
	 * @param response
	 * @param dataGri
	 */

	@RequestMapping(params = "rate_datagrid")
	public void rate_datagrid(TasksEntity tasks,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		
		String type = request.getParameter("type");
		String fbcph = request.getParameter("fbcph");
		String fwlname = request.getParameter("fwlname");
		String createDate_begin = request.getParameter("createDate_begin");
		String createDate_end = request.getParameter("createDate_end");
		
		String instockDate_begin = request.getParameter("instockDate_begin");
		String instockDate_end = request.getParameter("instockDate_end");
		
		
		CriteriaQuery cq = new CriteriaQuery(TasksEntity.class, dataGrid);
		//查询条件组装器
		// 平均成材率
	//	String sql = "select AVG(CONVERT(numeric(19,2), (SUBSTRING(frate,1,CHARINDEX('%',frate,1)-1)))) avg_frate from t_tasks where fstate = '1'";
		String sql = "select cast(Convert(decimal(28,2),round(SUM(Convert(decimal(18,2),isnull(fcpzl,0)))/SUM(Convert(decimal(18,2),isnull(fweight,0)))*100,2)) as varchar(10)) + '%' avg_frate from t_tasks where fstate = '1' and len(fcpzl) > 0 ";
		if(type.equals("S")){
		cq.like("fbcph", "S%");	
		sql += " and fbcph like 'S%'";
		}else if(type.equals("Y")){
		cq.like("fbcph", "Y%");
		sql += " and fbcph like 'Y%'";
		}
		if(StringUtil.isNotEmpty(fwlname)){
			sql += " and fwlname like '%" + fwlname + "%'";
		}
		if(StringUtil.isNotEmpty(fbcph)){
			sql += " and fbcph like '%" + fbcph + "%'";
		}
		if(StringUtil.isNotEmpty(createDate_begin)){
			sql += " and create_Date >= '" + createDate_begin + "'";
		}
		if(StringUtil.isNotEmpty(createDate_end)){
			sql += " and create_Date <= '" + createDate_end + "'";
		}
		
		
		if(StringUtil.isNotEmpty(instockDate_begin)){
			sql += " and instockDate >= '" + instockDate_begin + "'";
		}
		if(StringUtil.isNotEmpty(instockDate_end)){
			sql += " and instockDate <= '" + instockDate_end + "'";
		}
		
		cq.eq("fstate", "1");
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tasks, request.getParameterMap());
		//List<TasksEntity> taskss = this.tasksService.getListByCriteriaQuery(cq,false);
		this.tasksService.getDataGridReturn(cq, true);
		// 查询平均成材率语句
		String avg_frate = String.valueOf(tasksService.findOneForJdbc(sql).get("avg_frate"));
		avg_frate = avg_frate == "null" ? "":avg_frate; 
		dataGrid.setFooter("fbcph:合计,fweight,fcpzl,frate:" + avg_frate);
		TagUtil.datagrid(response, dataGrid);
	}
	/**
	 * 删除工序汇报任务单
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(TasksEntity tasks, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		tasks = systemService.getEntity(TasksEntity.class, tasks.getFid());
		message = "工序汇报任务单删除成功";
		tasksService.delete(tasks);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加工序汇报任务单
	 * 
	 * @param tasks
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(TasksEntity tasks, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(tasks.getFid())) {
			message = "工序汇报任务单更新成功";
			TasksEntity t = tasksService.get(TasksEntity.class, tasks.getFid());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(tasks, t);
				tasksService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "工序汇报任务单更新失败";
			}
		} else {
			message = "工序汇报任务单添加成功";
			
			
			
			ProcessReportEntity processReport=new ProcessReportEntity();
			processReport.setCprocess("p2_1");			
			processReport.setFbcph(tasks.getFbcph());
			processReport.setFtrhd(tasks.getFthick());
			processReport.setFtrkd(tasks.getFwidth());
			processReport.setFtrzl(tasks.getFweight());
			processReport.setFworkshop(tasks.getFworkshop());
			processReport.setFwlname(tasks.getFwlname());
			processReport.setFstate("0");
			processReportService.save(processReport);
			
			
			ProcessEntity entity=new ProcessEntity();
			entity.setFbcph(tasks.getFbcph());
			entity.setFprocess("p1");
			processService.save(entity);
			
			ProcessEntity entity2=new ProcessEntity();
			entity2.setFreportid(processReport.getId());
			entity2.setFbcph(tasks.getFbcph());
			entity2.setFprocess("p2_1");
			processService.save(entity2);
			
			tasks.setfNextReportId(processReport.getId());
			tasksService.save(tasks);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}
    
	/**
	 * 
	 * 反下单
	 * @param processReport
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "returnPlace")
	@ResponseBody
	public AjaxJson returnPlace(TasksEntity tasks, HttpServletRequest request)
	{

		AjaxJson j = new AjaxJson();

		// 获取任务单的id 、本厂批号和首道工序id
		String id = request.getParameter("id");
		String fbcph = request.getParameter("fbcph");
		String fNextReportId = request.getParameter("fNextReportId");

		String sql = " select fstate from  t_processreport where id = ? " ;
		
		String sql2 = " delete from t_process where freportid = ? ";

		String sql3 = " delete from t_process where fbcph = ? and fprocess = 'p1' ";

		if (StringUtil.isNotEmpty(fNextReportId))
		{

			String fstate = String.valueOf(systemService.findOneForJdbc(sql, fNextReportId).get("fstate"));

			// 反下单之前 先判断首道工序完工否
			if (fstate.equals("2"))
			{

				message = "反下单失败，该卷号在首道工序已完工";
				j.setMsg(message);
				return j;

			} else if (fstate.equals("4"))
			{

				message = "反下单失败，该卷号在首道工序已装炉";
				j.setMsg(message);
				return j;

			}  else if (fstate.equals("3"))
			{

				message = "反下单失败，该卷号在首道工序已分卷";
				j.setMsg(message);
				return j;

			}else   // 反下单
			{
				systemService.deleteEntityById(ProcessReportEntity.class, fNextReportId); // 删工序汇报记录
				systemService.deleteEntityById(TasksEntity.class, id); // 删任务单

				systemService.executeSql(sql2, fNextReportId); // 删工序记录
				systemService.executeSql(sql3, fbcph);

				message = "反下单成功";
			}

		} else
		{

			message = "反下单失败";
		}

		j.setMsg(message);
		return j;
	}


	/**
	 *
	 * 反下单
	 * @param processReport
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "cancel")
	@ResponseBody
	public AjaxJson cancel(String fbcph, HttpServletRequest request)
	{

		AjaxJson j = new AjaxJson();

		// 获取任务单的id 、本厂批号和首道工序id

		//String fbcph = request.getParameter("fbcph");


		String sql = " select fstate from  t_processreport where id = ? " ;

		String sql2 = " delete from t_process where freportid = ? ";

		String sql3 = " delete from t_process where fbcph = ? and fprocess = 'p1' ";

		String sql1="select fid,fNextReportId from t_tasks where fbcph=? ";
		Map<String,Object> task=systemService.findOneForJdbc(sql1, fbcph);

		String fNextReportId=task.get("fNextReportId").toString();

		if (StringUtil.isNotEmpty(fNextReportId))
		{

			String fstate = String.valueOf(systemService.findOneForJdbc(sql, fNextReportId).get("fstate"));

			// 反下单之前 先判断首道工序完工否
			if (fstate.equals("2"))
			{

				message = "反下单失败，该卷号在首道工序已完工";
				j.setMsg(message);
				return j;

			} else if (fstate.equals("4"))
			{

				message = "反下单失败，该卷号在首道工序已装炉";
				j.setMsg(message);
				return j;

			}  else if (fstate.equals("3"))
			{

				message = "反下单失败，该卷号在首道工序已分卷";
				j.setMsg(message);
				return j;

			}else   // 反下单
			{
				systemService.deleteEntityById(ProcessReportEntity.class, fNextReportId); // 删工序汇报记录
				systemService.deleteEntityById(TasksEntity.class, task.get("fid").toString()); // 删任务单

				systemService.executeSql(sql2, fNextReportId); // 删工序记录
				systemService.executeSql(sql3, fbcph);

				message = "反下单成功";
			}

		} else
		{

			message = "反下单失败";
		}

		j.setMsg(message);
		return j;
	}


	/**
	 * 工序汇报任务单列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(TasksEntity tasks, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(tasks.getFbcph())) {
			//tasks = tasksService.getEntity(TasksEntity.class, tasks.getFid());
			req.setAttribute("tasksPage", tasks);
		}
		return new ModelAndView("yongxin/tasks/tasks");
	}
	
	
	/**
	 * 工序汇报任务单列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "edit")
	public ModelAndView edit(TasksEntity tasks, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(tasks.getFid())) {
			tasks = tasksService.getEntity(TasksEntity.class, tasks.getFid());
			req.setAttribute("tasksPage", tasks);
		}
		return new ModelAndView("yongxin/tasks/edit_tasks");
	}
	
	
	
	/**
	 * 添加工序汇报
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "getProcess")
	@ResponseBody
	public AjaxJson getProcess(ProcessReportEntity processReport, HttpServletRequest request) {

		String fbcph=request.getParameter("fbcph");
		Object[] object=new Object[2];
		object[0]=fbcph;
		object[1]=fbcph+"-%";

		String sql="select tp.fbcph,tp.fprocess,tp.create_date,dic.typename,tp.freportid from t_process tp ";
		sql +=" left join (select tst.typecode,tst.typename from t_s_type tst,t_s_typegroup tstg ";
		sql +=" where tst.typegroupid=tstg.ID ";
		sql +=" and tstg.typegroupcode='ccgx') dic ";
		sql +=" on tp.fprocess=dic.typecode ";
		sql +=" where fbcph=? or fbcph like ? ";
		sql +=" order by tp.create_date ";
		/*
		String fbcph = request.getParameter("fbcph");
		String fisleft = request.getParameter("fisleft");
		String pid = request.getParameter("pid");
		String ofbcph = "";
		if (fbcph.indexOf("-") > 0)
		{
			ofbcph = fbcph.substring(0, fbcph.indexOf("-"));
		}
		Object[] object = new Object[3];
		object[0] = ofbcph;
		object[1] = fbcph;
		object[2] = fbcph;

		String sql = " select tp.fbcph,tp.fprocess,tp.create_date,dic.typename " + "   from t_process tp  "
				+ "   left join (select tst.typecode,tst.typename " + "    from t_s_type tst,t_s_typegroup tstg  "
				+ "    where tst.typegroupid=tstg.ID  and tstg.typegroupcode='ccgx') dic " + "     on tp.fprocess=dic.typecode      "
				+ "      where tp.fbcph=? and create_date< ( " + "		select min(create_date) from t_process t1 where t1.fbcph=? "
				+ "      ) " + " union " + "   select tp.fbcph,tp.fprocess,tp.create_date,dic.typename " + "   from t_process tp  "
				+ "   left join (select tst.typecode,tst.typename " + "    from t_s_type tst,t_s_typegroup tstg  "
				+ "    where tst.typegroupid=tstg.ID  and tstg.typegroupcode='ccgx') dic " + "     on tp.fprocess=dic.typecode     "
				+ "  where tp.fbcph=?    order by tp.create_date ";
		*/
		ProcessJsonEntity processJsonEntity=null;
		 try{
			 List<Map<String,Object>> maplist=systemService.findForJdbc(sql, object);
			 processJsonEntity= getProcessJson(maplist);
		 }
		 catch(Exception ex){
			 ex.printStackTrace();
		 }
		 finally{
			
		 }
		 AjaxJson j = new AjaxJson();
		 j.setObj(processJsonEntity);
		 
		return j;
	}
	
	
	private ProcessJsonEntity getProcessJson(List<Map<String,Object>> list){
		
		//demo_node_1:{name:"任务单<br>(YJ1503001)",left:67,top:71,type:"start",width:150,height:24},
		ProcessJsonEntity processJsonEntity=new ProcessJsonEntity();
		for (Map<String, Object> map : list) {
			String fbcph=map.get("fbcph").toString();
			String fprocess=map.get("fprocess").toString();
			String ftypename=map.get("typename").toString();
			String freportid=map.get("freportid")==null ? "" : map.get("freportid").toString();
			
			String nodename=ftypename+"<br>("+fbcph+")";
			
			
			ProcessNodeJsonEntity node=new ProcessNodeJsonEntity();
			node.setFbcph(fbcph);
			node.setName(nodename);
			node.setType("node");
			node.setProcess(fprocess);
			
			Object[] object=new Object[1];
			object[0]=fbcph;
			int sqltype=1;
			String sql="select fwlname '物料名称',fyclh '原厂炉号',fylghs '原料供货商',fthick '厚度',fwidth '宽度',fweight '重量',SUBSTRING(CONVERT(varchar,create_date,120),0,11) '制单日期' from t_tasks where fbcph= ? ";
			if(StringUtil.isNotEmpty(freportid)){
				object[0]=freportid;
				sql="select fbcph '<li>本厂批号',fmatchinegroup '<li>机组',ftrhd '<li>投入厚度',ftrkd '<li>投入宽度',ftrzl '<li>投入重量'," +
					"fcchd '<li class=\"field1\">产出厚度', fcckd '<li class=\"field1\">产出宽度', fcczl '<li class=\"field1\">产出重量'," +
					"fbfzl '<li class=\"field1\">报废重量',fnote '<li class=\"field1\">质量情况',frate '<li class=\"field1\">成材率',SUBSTRING(CONVERT(varchar,update_date,120),0,17) '<li class=\"field1\"> 汇报日期' " +
					"from t_processreport where id=? ";
				
				sqltype=2;
			}
			
			 try{
				 List<Map<String,Object>> maplist=systemService.findForJdbc(sql, object);
				 String details=getProcessDetailJson(maplist,sqltype);
				 node.setDetails(details);
			 }
			 catch(Exception ex){
				 node.setDetails("没有数据...");
			 }
			
			processJsonEntity.addNode(node);
			
		}
		
		
		
		return processJsonEntity;
	}
	
	
	private String getProcessDetailJson(List<Map<String,Object>> list,int sqltype){
		
		String jsonString = JSON.toJSONString(list);
		
		if(sqltype==1){
			jsonString=jsonString.replaceAll(",", "<br>");
			jsonString=jsonString.replaceAll("\"", "");
			jsonString=jsonString.replaceAll("[{}\\[\\]]", "");
			jsonString=jsonString.concat("<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
		}
		else{
			jsonString=jsonString.replaceAll(",", "</li>");
			jsonString=jsonString.replaceAll("\"", "");
			jsonString=jsonString.replace("[{", "<ul>");
			jsonString=jsonString.replace("}]", "</li></ul>");
			jsonString=jsonString.replace("\\", "");
		}
		
		
		
		return jsonString;
	}
	
	
	
	/**
	 * 导出excel 使用模板
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXlsByT")
	public void exportXlsByT(TasksEntity tasks,HttpServletRequest request,HttpServletResponse response, DataGrid dataGrid) {
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = null;
		OutputStream fOut = null;
		try {
			codedFileName = "成材率报表";
			// 根据浏览器进行转码，使其支持中文文件名
			if (BrowserUtils.isIE(request)) {
				response.setHeader(
						"content-disposition",
						"attachment;filename="
								+ java.net.URLEncoder.encode(codedFileName,
										"UTF-8") + ".xlsx");//这个地方要修改成和模板一样的文件类型
			} else {
				String newtitle = new String(codedFileName.getBytes("UTF-8"),
						"ISO8859-1");
				response.setHeader("content-disposition",
						"attachment;filename=" + newtitle + ".xlsx");
			}
			
			
			
			Map<String,Object> map = new HashMap<String, Object>();
			
			
			// 产生工作簿对象
			Workbook workbook = null;
			CriteriaQuery cq = new CriteriaQuery(TasksEntity.class, dataGrid);
			
			String type = request.getParameter("type");
			//查询条件组装器
			if (type.equals("S"))
			{
				cq.like("fbcph", "S%");
				map.put("steelType", "不锈钢");
				
			} else if (type.equals("Y"))
			{
				cq.like("fbcph", "Y%");
				map.put("steelType", "碳钢");
				
			}else if(type.equals("ALL")){
				map.put("steelType", "碳钢不锈钢");
			}
			
			cq.eq("fstate", "1");
			org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tasks, request.getParameterMap());
			List<TasksEntity> taskss = this.tasksService.getListByCriteriaQuery(cq,false);
			
			
			workbook = new ExcelExportOfTemplateUtil().createExcleByTemplate(new TemplateExportParams("export/template/exportTemp2.xlsx"),
					TasksEntity.class, taskss,map);
			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {
				
			}
		}
	}



	/**
	 * 检查任务单是否已经执行
	 *
	 * @param fbcph
	 * @return
	 */
	@RequestMapping(params = "checkTask")
	@ResponseBody
	public AjaxJson checkTask(String fbcph) {


		Object[] object=new Object[2];
		object[0]=fbcph;
		object[1]=fbcph+"-%";

		String sql="select tp.fbcph,tp.fprocess,tp.create_date,dic.typename,tp.freportid from t_process tp ";
		sql +=" left join (select tst.typecode,tst.typename from t_s_type tst,t_s_typegroup tstg ";
		sql +=" where tst.typegroupid=tstg.ID ";
		sql +=" and tstg.typegroupcode='ccgx') dic ";
		sql +=" on tp.fprocess=dic.typecode ";
		sql +=" where fbcph=? or fbcph like ? ";
		sql +=" order by tp.create_date ";

		ProcessJsonEntity processJsonEntity=null;
		try{
			List<Map<String,Object>> maplist=systemService.findForJdbc(sql, object);
			processJsonEntity= getProcessJson(maplist);
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		finally{

		}
		AjaxJson j = new AjaxJson();
		j.setObj(processJsonEntity);

		return j;
	}
}
